import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Author: Daniel Michalski
 * Date: 11.04.13
 */

public class Main {
    public static void main(String[] args) {
        //inetAdressTest();
        //socketTest();
        whoIsTest();
    }

    private static void inetAdressTest() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            System.out.println(address);

            address = InetAddress.getByName("www.HerbSchildt.com");
            System.out.println(address);

            InetAddress[] SW = InetAddress.getAllByName("www.wp.com");
            for (InetAddress inetAddress : SW) {
                System.out.println(inetAddress);
            }
        } catch (UnknownHostException e) {
            System.out.println("Błąd UknowHostException: " + e);
        }
    }

    private static void socketTest() {
        try (Socket socket = new Socket("www.wp.pl", 80))
        {
            System.out.println(socket.isConnected());
        } catch (IOException e) {
            System.out.println("Błąd: " + e);
        }
    }

    private static void whoIsTest(){
        try (Socket s = new Socket("whois.internic.net", 43))
        {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();

            String str = "nhl.com\n";

            byte[] buff = str.getBytes();

            out.write(buff);

            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
        } catch (IOException e) {
            System.out.println("Błąd: " + e);
        }
    }
}
